import vue from "vue";

const formatDate = (value) => {
    const s = 1000, m = 60 * s, h = 60 * m, d = 24 * h, mo = 30 * d, y = 365 * d
    const fdate = [
        {
            time: y,
            text: '年'
        }, {
            time: mo,
            text: '月'
        }, {
            time: d,
            text: '天'
        }, {
            time: h,
            text: '小时'
        }, {
            time: m,
            text: "分钟"
        }, {
            time: s,
            text: '秒'
        }
    ]
    const time = Date.now() - new Date(value)
    for (let i = 0; i < fdate.length; i++) {
        const t = time / fdate[i].time
        if (t >= 1) {
            return Math.floor(t) + fdate[i].text + '前'
        }
    }
}
const formatDate2 = (value) => {
    let time = new Date(value)
    let Y = time.getFullYear()
    let M = (time.getMonth() + 1 + "").padStart(2, "0")
    let D = (time.getDate() + "").padStart(2, "0")
    return `${Y}年${M}月${D}日`
}

[formatDate, formatDate2].forEach(item => vue.filter(item.name, item))
